﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TTEngine.Framework.UI;

namespace voidbox.Code
{
    public static class Debuger
    {
        public static bool Enable
        {
            get
            {
                if (debug_Container == null)
                    return false;
                return debug_Container.Enable;
            }
            set
            {
                if (debug_Container == null)
                {
                    InitDebugUI();
                }
                debug_Container.Enable = value;
                debug_Logs.Enable = value;
            }

        }
        static UIContainer debug_Container;
        static UIContainer debug_Logs;
        static int LogCount = 0;
        public static void ClearLog()
        {
            LogCount = 0;
            for (var i = 0; i < debug_Logs.GetChildCount(); i++)
            {
                var uilog = debug_Logs.GetChild(i) as UILabel;
                uilog.Text = "";
            }
        }
        public static void AddLog(string text)
        {
            if(LogCount==20)
            {
                for(var i=0;i<LogCount-1;i++)
                {
                    var uilog = debug_Logs.GetChild(i) as UILabel;
                    var uilogNext = debug_Logs.GetChild(i+1) as UILabel;
                    uilog.Text = uilogNext.Text;
                }
                LogCount--;
            }
            if(LogCount<20)
            {
                var uilog =debug_Logs.GetChild(LogCount) as UILabel;
                LogCount++;
                uilog.Text=text;
            }
        }
        static void InitDebugUI()
        {
            debug_Container = new UIContainer();

            debug_Container.LocalRect.SetHPosByCenter(512);
            debug_Container.LocalRect.SetVPosByTopBorder(40, 4);
            Root.UIFramework.Canvas.AddChild(debug_Container);
            //添加一个背景框
            var scale9 = new UIScale9(Root.UIFramework.Resource.BlockBorderScale9);
            debug_Container.AddChild(scale9);
            scale9.LocalColor = new Color(0.3f, 0.6f, 0.8f, 1);
            scale9.LocalRect.SetAsFill();
            scale9.scale = 2.0f;

            //添加fps
            var title = new UILabel();
            title.Font = Root.UIFramework.Resource.AscII12;
            title.Text = "Debug Mode";
            title._fontScale = new System.Numerics.Vector2(2f, 2f);
            title.HAlign = UIHAlign.Middle;
            title.VAlign = UIVAlign.Middle;
            title.LocalRect.SetHPosByCenter(256);
            title.LocalRect.SetVPosByTopBorder(32);
            debug_Container.AddChild(title);

            //RefreshCode
            var btn = new UIButton(Root.UIFramework.Resource.BlockBorderScale9, Root.UIFramework.Resource.AscII12, 2.0f);
            btn.SetText("RELOAD CODE");
            btn.LocalRect.SetHPosByLeftBorder(160, 16);
            btn.LocalRect.SetVPosByTopBorder(32, 4);
            btn.OnClick = () =>
            {
                ClearLog();
                Root.AppStack.Peek().ReloadCode();
            };
            debug_Container.AddChild(btn);

            //ShowLog
            var btnLog = new UIButton(Root.UIFramework.Resource.BlockBorderScale9, Root.UIFramework.Resource.AscII12, 2.0f);
            btnLog.SetText("Clear Log");
            btnLog.LocalRect.SetHPosByRightBorder(160, 16);
            btnLog.LocalRect.SetVPosByTopBorder(32, 4);
            btnLog.OnClick = () =>
            {
                ClearLog();
            };
            debug_Container.AddChild(btnLog);


            debug_Logs = new UIContainer();

            debug_Logs.LocalRect.SetAsFill();
            debug_Logs.LocalRect.OffsetX1 = 16;
            debug_Logs.LocalRect.OffsetY2 = -16;
            Root.UIFramework.Canvas.AddChild(debug_Logs);
            for (var i = 0; i < 20; i++)
            {
                var uilog = new UILabel();
                uilog.Font = Root.UIFramework.Resource.AscII12;
                uilog.Text = "";// "Debug Mode " + i.ToString("d02");
                uilog._fontScale = new System.Numerics.Vector2(2f, 2f);
                uilog.HAlign = UIHAlign.Left;
                uilog.VAlign = UIVAlign.Middle;
                uilog.LocalRect.SetHPosFill();
                uilog.LocalRect.SetVPosByBottomBorder(24, (20 - i) * 28);
                debug_Logs.AddChild(uilog);
            }
        }
    }
}
